/*
  ivl_mul.c  -  implementation of multiplication functions

  This file is part of the IVL Library (LIBIVL).
  
  LIBIVL - library for interval computations 
  
  Copyright (c) 2007 Mateusz Paprocki

  The IVL Library is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published
  by the Free Software Foundation; either version 2.1 of the License, or
  (at your option) any later version.

  The IVL Library is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with the IVL Library; see the file LICENCE.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  Boston, MA 02110-1301, USA.
*/

#include "interval.h"

int ivl_mul(ivl_ptr ivl, ivl_srcptr x, ivl_srcptr y) {
	int error = 0, inexact = 0, inexact_left, inexact_right;
	mpfr_t tmp;
	
	if (DOM_UNDEFINED(x) || DOM_UNDEFINED(y))
		ivl->domain = IVL_UNDEFINED;
	else {
		if (mpfr_sgn(IVL_LEFT(x)) < 0) {
			if (mpfr_sgn(IVL_RIGHT(x)) <= 0) {
				if (mpfr_sgn(IVL_RIGHT(y)) > 0) {
					if (mpfr_sgn(IVL_LEFT(y)) >= 0) {
						inexact_left = mpfr_mul(IVL_LEFT(ivl), IVL_LEFT(x), IVL_RIGHT(y), GMP_RNDD);
						inexact_right = mpfr_mul(IVL_RIGHT(ivl), IVL_RIGHT(x), IVL_LEFT(y), GMP_RNDU);
					} else {
						inexact_left = mpfr_mul(IVL_LEFT(ivl), IVL_RIGHT(x), IVL_LEFT(y), GMP_RNDD);
						inexact_right = mpfr_mul(IVL_RIGHT(ivl), IVL_RIGHT(x), IVL_RIGHT(y), GMP_RNDU);
					}
				} else {
					inexact_left = mpfr_mul(IVL_LEFT(ivl), IVL_RIGHT(x), IVL_RIGHT(y), GMP_RNDD);
					inexact_right = mpfr_mul(IVL_RIGHT(ivl), IVL_LEFT(x), IVL_LEFT(y), GMP_RNDU);
				}
			} else {
				if (mpfr_sgn(IVL_RIGHT(y)) > 0) {
					if (mpfr_sgn(IVL_LEFT(y)) >= 0) {
						inexact_left = mpfr_mul(IVL_LEFT(ivl), IVL_LEFT(x), IVL_RIGHT(y), GMP_RNDD);
						inexact_right = mpfr_mul(IVL_RIGHT(ivl), IVL_RIGHT(x), IVL_RIGHT(y), GMP_RNDU);	
					} else {
						mpfr_init2(tmp, ivl_get_prec(ivl));
						
						inexact_left = mpfr_mul(IVL_LEFT(ivl), IVL_LEFT(x), IVL_RIGHT(y), GMP_RNDD);
						inexact_left |= mpfr_mul(tmp, IVL_RIGHT(x), IVL_LEFT(y), GMP_RNDD);
						
						if (mpfr_nan_p(IVL_LEFT(ivl)) || mpfr_nan_p(tmp))
							error = 1;
						else {																		
							mpfr_min(IVL_LEFT(ivl), IVL_LEFT(ivl), tmp, GMP_RNDD);  
						
							inexact_right = mpfr_mul(IVL_RIGHT(ivl), IVL_LEFT(x), IVL_LEFT(y), GMP_RNDU);												
							inexact_right |= mpfr_mul(tmp, IVL_RIGHT(x), IVL_RIGHT(y), GMP_RNDU);
							
							if (mpfr_nan_p(IVL_LEFT(ivl)) || mpfr_nan_p(tmp))
								error = 1;
							else						
								mpfr_max(IVL_RIGHT(ivl), IVL_RIGHT(ivl), tmp, GMP_RNDD);
						}
						
						mpfr_clear(tmp);
					}
				} else {
					inexact_left = mpfr_mul(IVL_LEFT(ivl), IVL_RIGHT(x), IVL_LEFT(y), GMP_RNDD);
					inexact_right = mpfr_mul(IVL_RIGHT(ivl), IVL_LEFT(x), IVL_LEFT(y), GMP_RNDU);
				}
			}
		} else {
			if (mpfr_sgn(IVL_RIGHT(y)) > 0) {
				if (mpfr_sgn(IVL_LEFT(y)) >= 0) {
					inexact_left = mpfr_mul(IVL_LEFT(ivl), IVL_LEFT(x), IVL_LEFT(y), GMP_RNDD);
					inexact_right = mpfr_mul(IVL_RIGHT(ivl), IVL_RIGHT(x), IVL_RIGHT(y), GMP_RNDU);
				} else {
					inexact_left = mpfr_mul(IVL_LEFT(ivl), IVL_RIGHT(x), IVL_LEFT(y), GMP_RNDD);
					inexact_right = mpfr_mul(IVL_RIGHT(ivl), IVL_RIGHT(x), IVL_RIGHT(y), GMP_RNDU);
				}
			} else {
				inexact_left = mpfr_mul(IVL_LEFT(ivl), IVL_RIGHT(x), IVL_LEFT(y), GMP_RNDD);
				inexact_right = mpfr_mul(IVL_RIGHT(ivl), IVL_LEFT(x), IVL_RIGHT(y), GMP_RNDU);
			}
		}

		if (IVL_NAN_P(ivl) || error)
			ivl->domain = IVL_UNDEFINED;
		else {
			ivl->domain = x->domain | y->domain;
			
			if (inexact_left)
				inexact += 1;
			if (inexact_right)
				inexact += 2;
		}
	}
	
	return inexact;	
}

int ivl_mul_u(ivl_ptr ivl, ivl_srcptr x, unsigned long y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {		
		inexact_left = mpfr_mul_ui(IVL_LEFT(ivl), IVL_LEFT(x), y, GMP_RNDD);
		inexact_right = mpfr_mul_ui(IVL_RIGHT(ivl), IVL_RIGHT(x), y, GMP_RNDU);

		IVL_CHECK(ivl, x->domain, inexact, inexact_left, inexact_right);		
	}
	
	return inexact;
}

int ivl_mul_s(ivl_ptr ivl, ivl_srcptr x, long y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {				
		if (y >= 0) {
			inexact_left = mpfr_mul_si(IVL_LEFT(ivl), IVL_LEFT(x), y, GMP_RNDD);
			inexact_right = mpfr_mul_si(IVL_RIGHT(ivl), IVL_RIGHT(x), y, GMP_RNDU);
		} else {
			inexact_left = mpfr_mul_si(IVL_LEFT(ivl), IVL_RIGHT(x), y, GMP_RNDD);
			inexact_right = mpfr_mul_si(IVL_RIGHT(ivl), IVL_LEFT(x), y, GMP_RNDU);
		}

		IVL_CHECK(ivl, x->domain, inexact, inexact_left, inexact_right);		
	}
	
	return inexact;		
}

int ivl_mul_d(ivl_ptr ivl, ivl_srcptr x, double y) {
	int inexact = 0, inexact_left, inexact_right;
	mpfr_t tmp;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {
		mpfr_init2(tmp, 53);
		
		if (y >= 0) {
			inexact_left = mpfr_set_d(tmp, y, GMP_RNDD);
			inexact_left |= mpfr_mul(IVL_LEFT(ivl), IVL_LEFT(x), tmp, GMP_RNDD);
			
			inexact_right = mpfr_set_d(tmp, y, GMP_RNDU);
			inexact_right |= mpfr_mul(IVL_RIGHT(ivl), IVL_RIGHT(x), tmp, GMP_RNDU);
		} else {
			inexact_left = mpfr_set_d(tmp, y, GMP_RNDD);
			inexact_left |= mpfr_mul(IVL_LEFT(ivl), IVL_RIGHT(x), tmp, GMP_RNDD);
			
			inexact_right = mpfr_set_d(tmp, y, GMP_RNDU);
			inexact_right |= mpfr_mul(IVL_RIGHT(ivl), IVL_LEFT(x), tmp, GMP_RNDU);
		}
		
		mpfr_clear(tmp);
		
		IVL_CHECK(ivl, x->domain, inexact, inexact_left, inexact_right);		
	}
		
	return inexact;
}

int ivl_mul_z(ivl_ptr ivl, ivl_srcptr x, mpz_srcptr y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {		
		if (mpz_sgn(y) >= 0) {
			inexact_left = mpfr_mul_z(IVL_LEFT(ivl), IVL_LEFT(x), y, GMP_RNDD);
			inexact_right = mpfr_mul_z(IVL_RIGHT(ivl), IVL_RIGHT(x), y, GMP_RNDU);
		} else {
			inexact_left = mpfr_mul_z(IVL_LEFT(ivl), IVL_RIGHT(x), y, GMP_RNDD);
			inexact_right = mpfr_mul_z(IVL_RIGHT(ivl), IVL_LEFT(x), y, GMP_RNDU);
		}
		
		IVL_CHECK(ivl, x->domain, inexact, inexact_left, inexact_right);		
	}
	
	return inexact;
}

int ivl_mul_q(ivl_ptr ivl, ivl_srcptr x, mpq_srcptr y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {
		if (mpq_sgn(y) >= 0) {
			inexact_left = mpfr_mul_q(IVL_LEFT(ivl), IVL_LEFT(x), y, GMP_RNDD);
			inexact_right = mpfr_mul_q(IVL_RIGHT(ivl), IVL_RIGHT(x), y, GMP_RNDU);
		} else {
			inexact_left = mpfr_mul_q(IVL_LEFT(ivl), IVL_RIGHT(x), y, GMP_RNDD);
			inexact_right = mpfr_mul_q(IVL_RIGHT(ivl), IVL_LEFT(x), y, GMP_RNDU);
		}
		
		IVL_CHECK(ivl, x->domain, inexact, inexact_left, inexact_right);
	}
	
	return inexact;
}

int ivl_mul_f(ivl_ptr ivl, ivl_srcptr x, mpfr_srcptr y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {
		if (mpfr_sgn(y) >= 0) {
			inexact_left = mpfr_mul(IVL_LEFT(ivl), IVL_LEFT(x), y, GMP_RNDD);
			inexact_right = mpfr_mul(IVL_RIGHT(ivl), IVL_RIGHT(x), y, GMP_RNDU);
		} else {
			inexact_left = mpfr_mul(IVL_LEFT(ivl), IVL_RIGHT(x), y, GMP_RNDD);
			inexact_right = mpfr_mul(IVL_RIGHT(ivl), IVL_LEFT(x), y, GMP_RNDU);
		}
		
		IVL_CHECK(ivl, x->domain, inexact, inexact_left, inexact_right);				
	}
	
	return inexact;
}

int ivl_mul_2exp(ivl_ptr ivl, ivl_srcptr x, unsigned long exp) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {		
		inexact_left = mpfr_mul_2ui(IVL_LEFT(ivl), IVL_LEFT(x), exp, GMP_RNDD);
		inexact_right = mpfr_mul_2ui(IVL_RIGHT(ivl), IVL_RIGHT(x), exp, GMP_RNDU);		

		IVL_CHECK(ivl, x->domain, inexact, inexact_left, inexact_right);				
	}
	
	return inexact;
}

